package com.bw.apps.net

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.apps.R
import com.bw.apps.net.entity.AppMultipleEntity
import com.bw.quickadapter.BaseQuickMultipleAdapter
import com.bw.quickadapter.BaseViewHolder

class NetAppsAdapter : BaseQuickMultipleAdapter<AppMultipleEntity>(){
    companion object{
        const val TITLETYPE = 100
        const val VALUETYPE = 200
    }

    override fun getMultipleSpanSize(position: Int, recyclerView: RecyclerView?): Int {
        val layoutmanager = recyclerView!!.layoutManager
        if (layoutmanager !is GridLayoutManager){
            return 1
        }
        if (data[position].getType() == TITLETYPE)
            return layoutmanager.spanCount
        return 1
    }

    override fun convert(holder: BaseViewHolder?, item: AppMultipleEntity?) {
        item!!.let {
            if (it.getType() == TITLETYPE){
                holder!!.setText(R.id.net_apps_title_title,it.title)
            }else  if (it.getType() == VALUETYPE){
                holder!!.setImage(R.id.net_apps_child_img,it.appIcon)
                holder!!.setText(R.id.net_apps_child_title,it.appName)
            }
        }
    }

    override fun createItemVIew(viewType: Int, parent: ViewGroup?): View {
        return if (viewType == 100){
            LayoutInflater.from(parent!!.context).inflate(R.layout.item_new_apps_title,parent,false)
        } else{
            LayoutInflater.from(parent!!.context).inflate(R.layout.item_net_apps_child,parent,false)
        }
    }

    override fun getItemType(position: Int): Int {
        return data.get(position).getType()
    }
}